﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.EffectEditor
{
    public partial class ResourceDialog : Form
    {
        public ResourceDialog()
        {
            InitializeComponent();
        }

        private String _selectedResource = "";
        public String SelectedResource
        {
            get { return _selectedResource; }
            set
            {
                _selectedResource = value;
                texturePreview.SelectedTexture = value;
                Invalidate();
            }
        }

        public void FillList(Jemgine.Engine.EpisodeContentManager Content)
        {
            texturePreview.Initialize(Content);

            //listView1.LargeImageList = new ImageList();
            foreach (var Item in Content.Manifest.Entries)
            {
                if (Item.Type == ContentManifestExtensions.ContentType.Texture)
                {
                    //Texture2D texture = Content.GetTexture(Item.Name);

                    //// This only works for 32bbpARGB for the bitmap and Color for the texture, since these formats match.
                    //// Because they match, we can simply have Marshal copy over the data, otherwise we'd need to go over
                    //// each pixel and do the conversion manually (or through some trick I'm unaware off).

                    //byte[] textureData = new byte[4 * texture.Width * texture.Height];
                    //texture.GetData<byte>(textureData);

                    //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(
                    //               texture.Width, texture.Height,
                    //               System.Drawing.Imaging.PixelFormat.Format32bppArgb
                    //             );

                    //System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(
                    //               new System.Drawing.Rectangle(0, 0, texture.Width, texture.Height),
                    //               System.Drawing.Imaging.ImageLockMode.WriteOnly,
                    //               System.Drawing.Imaging.PixelFormat.Format32bppArgb
                    //             );

                    //IntPtr safePtr = bmpData.Scan0;
                    //System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                    //bmp.UnlockBits(bmpData);

                    //listView1.LargeImageList.Images.Add(Item.Name, bmp);
                    var Entry = new ListViewItem(Item.Name, "");
                    resourceList.Items.Add(Entry);
                }
            }
        }

        private void resourceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (resourceList.SelectedItems.Count <= 0)
                SelectedResource = "";
            else
                SelectedResource = resourceList.SelectedItems[0].Text;
            Invalidate();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
